﻿using System;
using System.Collections.Generic;
using System.Text;

namespace StudentManagement.Tools
{
    public enum SqlWhereOperator
    {
        /// <summary>
        /// 并且
        /// </summary>
        AND = 1,

        /// <summary>
        /// 或者
        /// </summary>
        OR = 2
    }

    /// <summary>
    /// SQL的条件集合
    /// </summary>
    public class SqlWhereList : List<string>
    {
        /// <summary>
        /// 将条件列表转为SQL的条件字符串
        /// </summary>
        /// <param name="whereOperator">拼接在一起的条件运算符</param>
        /// <param name="isAddWhere">是否在前缀加上where</param>
        /// <returns></returns>
        public string ToSqlWhwere(SqlWhereOperator whereOperator, bool isAddWhere = true)
        {
            StringBuilder whereBuilder = new StringBuilder();
            if (Count > 0)
            {
                if (Count == 1)
                {
                    whereBuilder.Append($" {this[0]} ");
                }
                else
                {
                    whereBuilder.AppendJoin($" {whereOperator} ", this);
                }
            }

            if (whereBuilder.Length > 0 && isAddWhere)
            {
                whereBuilder.Insert(0, " WHERE ");
            }

            return whereBuilder.ToString();
        }

        /// <summary>
        /// 转换成SQL条件表示
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return ToSqlWhwere(SqlWhereOperator.AND);
        }
    }
}
